/*
 * @(#)PNTransformer.java
 * Date 2013-09-27
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.hl7v3common.transformer;

import java.util.List;

import javax.enterprise.context.ApplicationScoped;

import org.apache.commons.lang3.StringUtils;

import org.hl7.v3.EnFamily;
import org.hl7.v3.EnGiven;
import org.hl7.v3.EnPrefix;
import org.hl7.v3.EnSuffix;
import org.hl7.v3.PN;

import us.hornerscorners.lamppost.hl7v3common.utils.HL7Utils;

import us.hornerscorners.lamppost.model.common.CodedElement;

import us.hornerscorners.lamppost.model.patient.PersonName;

import us.hornerscorners.lamppost.transformer.AbstractTransformer;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-09-27
 * @author         Jim Horner    
 */
@ApplicationScoped
public class PNTransformer extends AbstractTransformer<PersonName, PN> {

    /**
     * Constructs ...
     *
     */
    public PNTransformer() {
        super();
    }

    /**
     *
     * @param name
     * @return
     */
    @Override
    public PN transform(PersonName name) {

        PN result = new PN();

        CodedElement purpose = name.getPurpose();

        if ((purpose != null) && StringUtils.isNotBlank(purpose.getCode())) {

            result.getUse().add(purpose.getCode());
        }

        // Prefix
        String prefix = name.getPrefix();

        if (StringUtils.isNotBlank(prefix)) {

            EnPrefix enPrefix = new EnPrefix();

            enPrefix.getContent().add(prefix);

            result.getContent().add(HL7Utils.hl7Factory.createENPrefix(enPrefix));
        }

        // First Name
        String firstName = name.getFirstName();

        if (StringUtils.isNotBlank(firstName)) {

            EnGiven enGiven = new EnGiven();

            enGiven.getContent().add(firstName);

            result.getContent().add(HL7Utils.hl7Factory.createENGiven(enGiven));
        }

        // Last Name
        String lastName = name.getLastName();

        if (StringUtils.isNotBlank(lastName)) {

            EnFamily enFamily = new EnFamily();

            enFamily.getContent().add(lastName);

            result.getContent().add(HL7Utils.hl7Factory.createENFamily(enFamily));
        }

        // Middle Names
        List<String> middleNames = name.getMiddleNames();

        for (String middleName : middleNames) {

            EnGiven enGiven = new EnGiven();

            enGiven.getContent().add(middleName);

            result.getContent().add(HL7Utils.hl7Factory.createENGiven(enGiven));
        }

        // Suffix
        String suffix = name.getSuffix();

        if (StringUtils.isNotBlank(suffix)) {

            EnSuffix enSuffix = new EnSuffix();

            enSuffix.getContent().add(suffix);

            result.getContent().add(HL7Utils.hl7Factory.createENSuffix(enSuffix));
        }

        return result;
    }
}
